package com.link.base.base.auth.controller;

import com.link.base.base.auth.model.Authclient;
import com.link.base.base.auth.service.AuthclientService;
import com.link.base.base.gateway.service.GatewayService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 * 认证服务的Client相关接口
 *
 * @author 黄龙建
 * @version 1.0
 * @date: 2018-09-18
 */

@Controller
@RequestMapping("/link/authclient")
public class AuthclientController extends BasicController<Authclient> {
    @Resource
    private AuthclientService authclientService;

    @Resource
    private GatewayService gatewayService;

    @Override
    public BasicService<Authclient> getBasicService() throws Exception {
        return authclientService;
    }

    /**
     * @param record 实体对象
     * @return Map
     * @throws Exception 报错
     * @auther HuangLJ
     * @date 2018/11/8 4:01 PM
     * @comments 查询职责的Client
     */
    @RequestMapping(value = "/dutyOfClientList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> dutyOfClientList(@JsonParam Authclient record, @JsonParam QueryParams qps, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Authclient> list = null;
        try {
            qps.invokePreFilterAndSorter();
            Authclient t = (Authclient) BasicModel.transformClass(Authclient.class, qps);
            list = authclientService.queryClientByDutyPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * @param record 实体对象
     * @return Map
     * @throws Exception 报错
     * @auther HuangLJ
     * @date 2018/11/8 4:02 PM
     * @comments 查询职责下面，没有被选中的的client
     */
    @RequestMapping(value = "/unSelectClientList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectClientList(@JsonParam Authclient record, @JsonParam QueryParams qps, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Authclient> list = null;
        record.setQueryId(record.getId());
        try {
            Authclient t = BasicModel.transformClass(Authclient.class, qps);
            list = authclientService.queryUnSelectClientListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * @param authclient
     * @return Map
     * @throws Exception 报错
     * @auther wangdawei
     * @date 2018/10/14 4:02 PM
     * @comments 刷新内存中的aes密钥
     */
    @RequestMapping("/refresh")
    public Map<String,Object> refresh(@JsonParam Authclient authclient) {
        Map<String,Object> result = new HashMap<>(6);
        try {
            gatewayService.refreshAllCache(authclient.getScope());
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
